package com.mbl.base.modular.mall.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbl.base.config.enums.ResultEnum;
import com.mbl.base.core.common.constant.MblConstantFactory;
import com.mbl.base.core.common.dto.MblResult;
import com.mbl.base.core.common.page.LayuiPageFactory;
import com.mbl.base.core.modular.task.UtilTask;
import com.mbl.base.core.utils.UtilRandom;
import com.mbl.base.modular.mall.constant.CouponUserConstant;
import com.mbl.base.modular.mall.constant.GrouponConstant;
import com.mbl.base.modular.mall.entity.*;
import com.mbl.base.modular.mall.mapper.OrderMapper;
import com.mbl.base.modular.mall.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mbl.base.modular.mall.task.OrderUnpaidTask;
import com.mbl.base.modular.mall.utils.OrderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author lwj
 * @since 2020-03-21
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Override
    public int updateWithOptimisticLocker(Order order) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("update_time",order.getUpdateTime());
        updateWrapper.eq("id",order.getId());
        final boolean update = update(order, updateWrapper);
        return update ? 1 : 0;
    }

    @Override
    public IPage<Order> queryByOrderStatus(Long userId, List<Short> orderStatus) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        if (orderStatus != null) {
            queryWrapper.in("order_status",orderStatus);
        }
        final Page page = LayuiPageFactory.defaultPage();
        return page(page, queryWrapper);
    }

    @Override
    public Order findBySn(String orderSn) {
        return query().eq("order_sn", orderSn).one();
    }
}
